package flc.ast.dialog;

import static com.blankj.utilcode.util.StringUtils.getString;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.AddVideoAdapter;
import flc.ast.databinding.DialogAddVideoBinding;
import flc.ast.databinding.DialogSetAnswerBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.SPUtil;

public class AddVideoDialog extends BaseSmartDialog<DialogAddVideoBinding> implements View.OnClickListener, OnItemClickListener {

    private List<SelectMediaEntity> listAll = new ArrayList<>();//全部数据
    private List<SelectMediaEntity> listShow = new ArrayList<>();//搜索后展示的数据
    private List<SelectMediaEntity> listSel = new ArrayList<>();//选择的集合
    private AddVideoAdapter videoAdapter;
    private boolean isSearch;


    public AddVideoDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_add_video;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvDialogAddVideoAdd.setOnClickListener(this);
        mDataBinding.ivDialogAddVideoSearch.setOnClickListener(this);
        mDataBinding.tvDialogAddVideoCancel.setOnClickListener(this);

        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(getContext(), 2));
        videoAdapter = new AddVideoAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);

        mDataBinding.tvDialogAddVideoAdd.setText(getString(R.string.add_num, listSel.size()));

        getData();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivDialogAddVideoSearch://搜索
                isSearch = true;
                searchVideo();
                mDataBinding.ivDialogAddVideoSearch.setVisibility(View.INVISIBLE);
                mDataBinding.tvDialogAddVideoCancel.setVisibility(View.VISIBLE);
                break;
            case R.id.tvDialogAddVideoCancel://取消
                isSearch = false;
                mDataBinding.etDialogAddVideoText.setText("");
                mDataBinding.etDialogAddVideoText.clearFocus();
                searchVideo();
                mDataBinding.ivDialogAddVideoSearch.setVisibility(View.VISIBLE);
                mDataBinding.tvDialogAddVideoCancel.setVisibility(View.INVISIBLE);
                break;
            case R.id.tvDialogAddVideoAdd://添加
                if (listSel.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_video));
                    return;
                }
                dismiss();
                List<String> list = new ArrayList<>();
                for (SelectMediaEntity selectMediaEntity : listSel) {
                    list.add(selectMediaEntity.getPath());
                }
                listener.clickAdd(list);
                break;
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (videoAdapter.getItem(position).isChecked()) {
            videoAdapter.getItem(position).setChecked(false);
            listSel.remove(videoAdapter.getItem(position));
        } else {
            videoAdapter.getItem(position).setChecked(true);
            listSel.add(videoAdapter.getItem(position));
        }
        videoAdapter.notifyItemChanged(position);
        mDataBinding.tvDialogAddVideoAdd.setText(getString(R.string.add_num, listSel.size()));
    }

    //获取视频和图片数据
    private void getData() {
        listAll.clear();
        listShow.clear();
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(getContext(), PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    listAll.addAll(selectMediaEntities);
                    videoAdapter.setList(listAll);
                    mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.tvDialogAddVideoNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvVideoList.setVisibility(View.GONE);
                    mDataBinding.tvDialogAddVideoNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    //搜索视频
    private void searchVideo() {
        String text = mDataBinding.etDialogAddVideoText.getText().toString();
        if (TextUtils.isEmpty(text)) {
            videoAdapter.setList(listAll);
        } else {
            listShow.clear();
            for (SelectMediaEntity bean : listAll) {
                if (bean.getMediaName().contains(text)) {
                    listShow.add(bean);
                }
            }
            videoAdapter.setList(listShow);
        }
        if (listShow.size() > 0) {
            mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
            mDataBinding.tvDialogAddVideoNoData.setVisibility(View.GONE);
            mDataBinding.tvDialogAddVideoNoSearch.setVisibility(View.GONE);
        } else {
            mDataBinding.rvVideoList.setVisibility(View.GONE);
            if (TextUtils.isEmpty(text)) {
                mDataBinding.tvDialogAddVideoNoData.setVisibility(View.VISIBLE);
                mDataBinding.tvDialogAddVideoNoSearch.setVisibility(View.GONE);
            } else {
                mDataBinding.tvDialogAddVideoNoData.setVisibility(View.GONE);
                mDataBinding.tvDialogAddVideoNoSearch.setVisibility(View.VISIBLE);
            }
        }
        videoAdapter.notifyDataSetChanged();
    }


    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void clickAdd(List<String> list);
    }
}
